package swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MultipleListener implements MouseListener ,MouseMotionListener{
	private JFrame frame;
	private JLabel label;
	private JTextField southTxt;
	private JPanel centerPanel;
	private Container contentPane;
	
	public MultipleListener(){
		frame = new JFrame("Multiple Listener EX");
		frame.setSize(400,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		contentPane = frame.getContentPane();
		initGUI();
	}

	private void initGUI() {
		contentPane.setLayout(new BorderLayout());
		label = new JLabel("please drag or click mouse");
		contentPane.add(label,BorderLayout.NORTH);
		southTxt = new JTextField(80);
		contentPane.add(southTxt,BorderLayout.SOUTH);
		
		centerPanel = new JPanel();
//		centerPanel.setBackground(Color.RED);
		centerPanel.addMouseListener(this);
		centerPanel.addMouseMotionListener(this);
		contentPane.add(centerPanel,BorderLayout.CENTER);
	}
	
	public void go(){
		frame.show();
	}
	
	public static void main(String[] args) {
		(new MultipleListener()).go();
	}

	public void mouseClicked(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		String msg = "Mouse clicked:X="+x+",Y="+y;
		southTxt.setText(msg);
	}

	public void mouseEntered(MouseEvent e) {
		String msg = "the mouse entered.";
		southTxt.setText(msg);
	}

	public void mouseExited(MouseEvent e) {
		String msg = "the mouse exited";
		southTxt.setText(msg);
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseDragged(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		String msg = "Mouse dragged:X="+x+",Y="+y;
		southTxt.setText(msg);
	}

	public void mouseMoved(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		String msg = "Mouse moved:X="+x+",Y="+y;
		southTxt.setText(msg);
	}

}
